package com.kaifamiao.month05.day06;

public class Cook {

    private Cook() {

    }

    private static Cook cook;

    public static Cook getInstance() {
        if (cook == null) {
            cook = new Cook();
        }
        return cook;
    }

    private String name;
    public synchronized void make() {
        if (name != null) { // 已经有做好的蛋糕
            try {
                wait(); // 进入等待状态
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        name = String.valueOf(System.currentTimeMillis());
        notifyAll();
        System.out.println(Thread.currentThread().getName() + " 做了一个蛋糕 " + name);
    }

    public synchronized void eat() {
        if (name == null) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } else {
            System.out.println(Thread.currentThread().getName() + " 吃了一个蛋糕 " + name);
            name = null;
            notifyAll();
        }
    }
}
